package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyParkCarParkingAddBo;
import com.ruoyi.system.bo.ZsyyParkCarParkingQueryBo;
import com.ruoyi.system.bo.ZsyyParkCarParkingEditBo;
import com.ruoyi.system.domain.ZsyyParkCarParking;
import com.ruoyi.system.mapper.ZsyyParkCarParkingMapper;
import com.ruoyi.system.vo.ZsyyParkCarParkingVo;
import com.ruoyi.system.service.IZsyyParkCarParkingService;
import com.ruoyi.system.mapper.ZsyyParkCarParkingMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 车辆和车场关系Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyParkCarParkingServiceImpl extends ServicePlusImpl<ZsyyParkCarParkingMapper, ZsyyParkCarParking> implements IZsyyParkCarParkingService {
    @Autowired
    private ZsyyParkCarParkingMapper iZsyyParkCarParkingMapper;

    @Override
    public ZsyyParkCarParkingVo queryById(Long id){
        return getVoById(id, ZsyyParkCarParkingVo.class);
    }

    @Override
    public TableDataInfo<ZsyyParkCarParkingVo> queryPageList(ZsyyParkCarParkingQueryBo bo) {
        PagePlus<ZsyyParkCarParking, ZsyyParkCarParkingVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), ZsyyParkCarParkingVo.class);
        return PageUtils.buildDataInfo(result);
    }

    @Override
    public List<ZsyyParkCarParkingVo> queryList(ZsyyParkCarParkingQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyParkCarParkingVo.class);
    }

    private LambdaQueryWrapper<ZsyyParkCarParking> buildQueryWrapper(ZsyyParkCarParkingQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyParkCarParking> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, ZsyyParkCarParking::getUserId, bo.getUserId());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerId()), ZsyyParkCarParking::getCustomerId, bo.getCustomerId());
        lqw.eq(StrUtil.isNotBlank(bo.getPkGlobalid()), ZsyyParkCarParking::getPkGlobalid, bo.getPkGlobalid());
        lqw.eq(StrUtil.isNotBlank(bo.getCarLicenseNumber()), ZsyyParkCarParking::getCarLicenseNumber, bo.getCarLicenseNumber());
        lqw.eq(bo.getCustomerSeq() != null, ZsyyParkCarParking::getCustomerSeq, bo.getCustomerSeq());
        lqw.eq(bo.getCustomerRole() != null, ZsyyParkCarParking::getCustomerRole, bo.getCustomerRole());
        lqw.eq(bo.getCarNumber() != null, ZsyyParkCarParking::getCarNumber, bo.getCarNumber());
        lqw.eq(bo.getCarportNumber() != null, ZsyyParkCarParking::getCarportNumber, bo.getCarportNumber());
        lqw.eq(StrUtil.isNotBlank(bo.getCustomerRoomNumber()), ZsyyParkCarParking::getCustomerRoomNumber, bo.getCustomerRoomNumber());
        lqw.eq(bo.getCustomerStatus() != null, ZsyyParkCarParking::getCustomerStatus, bo.getCustomerStatus());
        lqw.eq(bo.getIsDeleted() != null, ZsyyParkCarParking::getIsDeleted, bo.getIsDeleted());
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyParkCarParkingAddBo bo) {
        ZsyyParkCarParking add = BeanUtil.toBean(bo, ZsyyParkCarParking.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyParkCarParkingEditBo bo) {
        ZsyyParkCarParking update = BeanUtil.toBean(bo, ZsyyParkCarParking.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyParkCarParking entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
